import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/pages/mine/user_store.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:get/get.dart';

class UpgradeDialog {
  static void upgradeDialog(
    BuildContext mContext, {
      Function? cancelTap,
      Function? confirmTap,
      String? version,
      String? versionContent,
      bool forcedUpgrade = false
    }
  ) {
    showDialog(
      barrierDismissible: false,
      context: mContext,
      builder: (BuildContext context) {
        return WillPopScope(
          onWillPop: () async => false,
          child: UpgradeDialogPage(
            cancelTap: cancelTap,
            confirmTap: confirmTap,
            version: version,
            versionContent: versionContent,
            forcedUpgrade: forcedUpgrade
          ),
        );
      }
    );
  }
}


class UpgradeDialogPage extends Dialog {
  UpgradeDialogPage({
    this.cancelTap,
    this.confirmTap,
    this.version,
    this.versionContent,
    this.forcedUpgrade = false
  });

  // 取消
  final Function? cancelTap;
  // 确认
  final Function? confirmTap;
  // 版本号
  final String? version;
  // 更新内容
  final String? versionContent;
  // 是否强制更新
  final bool forcedUpgrade;

  @override
  Widget build(BuildContext dContext) {
    return Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      //自定义dialog布局
      child: GetBuilder<UserStore>(
        builder: (userStore) {
          return Center(
            child: SingleChildScrollView(
              padding: EdgeInsets.symmetric(vertical: 20.w),
              physics: const BouncingScrollPhysics(),
              child: RichText(
                text: TextSpan(
                  children: [
                    WidgetSpan(
                      child: Stack(
                        alignment: Alignment.center,
                        clipBehavior: Clip.none,
                        children: [
                          SizedBox(
                            child: Column(
                              children: [
                                Container(
                                  margin: EdgeInsets.symmetric(horizontal: 50.w),
                                  width: double.infinity,
                                  decoration: BoxDecoration(
                                    color: Colors.white,
                                    borderRadius: BorderRadius.circular(8.w)
                                  ),
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      SizedBox(height: 80.w,),
            
                                      Container(
                                        alignment: Alignment.center,
                                        child: RichText(
                                          text: TextSpan(
                                            children: [
                                              TextSpan(
                                                style: TextStyle(fontSize: 16.sp, color: const Color(0xff333333), fontWeight: FontWeight.w500),
                                                text: '发现新版本 '
                                              ),
                                              TextSpan(
                                                style: TextStyle(fontSize: 16.sp, color: const Color(0xff333333), fontWeight: FontWeight.w500),
                                                text: 'V$version'
                                              )
                                            ]
                                          )
                                        )
                                      ),
            
                                      SizedBox(height: 16.w,),
            
                                      Container(
                                        padding: EdgeInsets.symmetric(horizontal: 15.w),
                                        child: Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Container(
                                              padding: EdgeInsets.symmetric(horizontal: 10.w),
                                              child: Text('$versionContent', style: TextStyle(fontSize: 14.sp, height: 1.5.w, color: const Color(0xff333333)), ),
                                            )
                                          ],
                                        ),
                                      ),
            
            
            
                                      SizedBox(height: 20.w,),
            
                                      if (userStore.downloadType == 'downloading') ...[
                                        Container(
                                          margin: EdgeInsets.only(bottom: 15.w),
                                          padding: EdgeInsets.symmetric(horizontal: 15.w),
                                          child: Row(
                                            children: [
                                              Expanded(
                                                child: ClipRRect(
                                                  borderRadius: BorderRadius.circular(10.r),
                                                  child: LinearProgressIndicator(
                                                    // value: 0.5,
                                                    value: userStore.downloadProgress,
                                                    minHeight: 10,
                                                    backgroundColor: Colors.grey.shade200,
                                                    valueColor: const AlwaysStoppedAnimation(Color(0xff499360)),
                                                  ),
                                                ),
                                              ),
            
                                              SizedBox(width: 10.w,),
            
                                              SizedBox(
                                                child: Text('${(userStore.downloadProgress * 100).toStringAsFixed(0)}%', style: TextStyle(fontSize: 14.sp, color: const Color(0xff499360)),),
                                              )
                                            ],
                                          ),
                                        )
                                      ] else if (userStore.downloadType == 'done') ...[
                                        Container(
                                          margin: EdgeInsets.only(bottom: forcedUpgrade == false ? 10.w : 15.w),
                                          alignment: Alignment.center,
                                          child: GestureDetector(
                                            behavior: HitTestBehavior.translucent,
                                            onTap: () {
                                              confirmTap?.call();
                                            },
                                            child: Container(
                                              decoration: BoxDecoration(
                                                color: const Color(0xff008BEC),
                                                borderRadius: BorderRadius.circular(6.r),
                                                gradient: const LinearGradient(
                                                  begin: Alignment(-0.06, 1.33),
                                                  end: Alignment(1.2, -0.66),
                                                  colors: [
                                                    Color(0xff499360),
                                                    Color(0xff67E58E),
                                                  ],
                                                  stops: [0, 1.0],
                                                ),
                                              ),
                                              width: 230.w,
                                              alignment: Alignment.center,
                                              padding: EdgeInsets.symmetric(vertical: 10.w),
                                              child: Text('重新下载', style: TextStyle(fontSize: 15.sp, color: Colors.white),),
                                            ),
                                          ),
                                        ),
                                      ] else ...[
                                        Container(
                                          margin: EdgeInsets.only(bottom: forcedUpgrade == false ? 10.w : 15.w),
                                          alignment: Alignment.center,
                                          child: GestureDetector(
                                            behavior: HitTestBehavior.translucent,
                                            onTap: () {
                                              confirmTap?.call();
                                            },
                                            child: Container(
                                              decoration: BoxDecoration(
                                                color: const Color(0xff008BEC),
                                                borderRadius: BorderRadius.circular(6.r),
                                                gradient: const LinearGradient(
                                                  begin: Alignment(-0.06, 1.33),
                                                  end: Alignment(1.2, -0.66),
                                                  colors: [
                                                    Color(0xff499360),
                                                    Color(0xff67E58E),
                                                  ],
                                                  stops: [0, 1.0],
                                                ),
                                              ),
                                              width: 230.w,
                                              alignment: Alignment.center,
                                              padding: EdgeInsets.symmetric(vertical: 10.w),
                                              child: Text('立即更新', style: TextStyle(fontSize: 15.sp, color: Colors.white),),
                                            ),
                                          ),
                                        ),
            
                                        
                                        if (forcedUpgrade == false) ...[
                                          Container(
                                            margin: EdgeInsets.only(bottom: 10.w),
                                            alignment: Alignment.center,
                                            child: GestureDetector(
                                              behavior: HitTestBehavior.translucent,
                                              onTap: () {
                                                Get.back();
                                              },
                                              child: Container(
                                                width: 230.w,
                                                alignment: Alignment.center,
                                                padding: EdgeInsets.symmetric(vertical: 10.w),
                                                child: Text('稍后更新', style: TextStyle(fontSize: 15.sp, color: const Color(0xff499360)),),
                                              ),
                                            ),
                                          ),
                                        ],
            
                                      ],
                                    ],
                                  ),
                                )
                              ],
                            ),
                          ),
            
                          Positioned(
                            top: -30.w,
                            child: Container(
                              width: MediaQuery.of(Get.context!).size.width,
                              height: 94.w,
                              padding: EdgeInsets.symmetric(horizontal: 50.w),
                              child: SizedBox(
                                child: Image.asset('assets/images/bbgx_img.png', fit: BoxFit.fill,),
                              ),
                            )
                          )
                        ],
                      )
                    ),
                  ]
                ),
              ),
            ),
          );
        }
      )
    );
  }
}